import { useStore as useMenuStore } from "@/store/menu";
import { useStore as useUserStore } from "@/store/user";
// 判断权限
export const judgeAuth = (myAuths: string) => {
  const menuStore = useMenuStore();
  const userStore = useUserStore();
  return userStore.isSuperAdmin || !myAuths || menuStore.state.auths.includes(myAuths ?? '');
}

// 权限 and or 语句解析
export const analysisAuthAndOr = (auth: any, authConfig: any): boolean => {
  const dealAuth: any = (e: any) => {
    // 字符串类型,判断权限
    if (typeof e === 'string') {
      return !e ||authConfig?.handler?.[e]?.hasAuth;
    }
    // 对象类型(单属性对象), 对象key可为and | or 
    else {
      // key为and时, value条件须全部符合条件 
      if (Object.keys(e)[0] === 'and') {
        return (Object.values(e)[0] as any[])?.every((x: any) => dealAuth(x));
      } 
      // key为and时, value条件只需符合其中一项
      else {
        return (Object.values(e)[0] as any[])?.some((x: any) => dealAuth(x));
      }
    }
  }
  return dealAuth(auth);
}